package ${package.Controller};


import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.xiaoxi.bms.common.exception.BMSException;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxi.bms.service.common.aspect.operation.OperationLog;
import com.xiaoxi.bms.common.constant.constant.OperationConstants;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName ${table.controllerName}
 * @Description
 * @Author ${author}
 * @Date ${date}
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/${table.entityPath}/v1")
public class ${table.controllerName} {

    @Resource
    private ${table.serviceName} ${table.serviceName?uncap_first};

	/**
     * 新增
     *
     * @param ${entity?uncap_first}
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_CREATE)
    @PostMapping("/add")
    public ApiResponse add(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            ${table.serviceName?uncap_first}.add(${entity?uncap_first});
       		return ApiResponse.ofSuccess(${entity?uncap_first});
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

	/**
     * 删除
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_DELETE)
  	@GetMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id){
        try {
            ${table.serviceName?uncap_first}.delete(id);
        	return ApiResponse.ofSuccess(id);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

	/**
     * 更新
     *
     * @param ${entity?uncap_first}
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_MODIFY)
    @PostMapping("/update")
    public ApiResponse update(@RequestBody ${entity} ${entity?uncap_first}){
        try {
            ${table.serviceName?uncap_first}.update(${entity?uncap_first});
        	return ApiResponse.ofSuccess(${entity?uncap_first});
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

	/**
     * 查询分页数据
     *
     * @param ${entity?uncap_first}
     * @param pageable
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_PAGE)
    @GetMapping("/findListByPage")
    public ApiResponse findListByPage(${entity} ${entity?uncap_first},Pageable pageable){
        try {
            IPage<${entity}> result = ${table.serviceName?uncap_first}.findListByPage(${entity?uncap_first}, pageable);
        	return ApiResponse.ofSuccess(result);
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

	/**
     * ID查询
     *
     * @param id
     * @return
     */
    @OperationLog(operation = OperationConstants.CODE_QUERY_ONE)
    @GetMapping("/findById/{id}")
    public ApiResponse findById(@PathVariable Long id){
        try {
            ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.findById(id);
            return ApiResponse.ofSuccess(${entity?uncap_first});
        } catch (BMSException e) {
            log.error(e.getMessage(), e);
            return ApiResponse.ofException(e);
        }
    }

}
